package tp_automates;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class SimpleState implements State {

	Map<Symbol, StateSet> map;
	String name;

	public SimpleState() {
		map = new HashMap<Symbol, StateSet>();
		name = "anon";
	}

	public SimpleState(String n) {
		map = new HashMap<Symbol, StateSet>();
		name = n;
	}

	public void addTransition(State end, Symbol transition) {
		if (map.containsKey(transition))
			map.get(transition).add(end);
		else {
			StateSet sts = new StateSet();
			sts.add(end);
			map.put(transition, sts);
		}
	}

	/**
	 * cf State.goStepByTransition(Symbol symbol)
	 */
	@Override
	public State goStepByTransition(Symbol symbol) {
		if (map.size() > 0) {

			Set<Symbol> cles = map.keySet();
			Iterator<Symbol> it = cles.iterator();
			while (it.hasNext()) {
				Symbol cle = it.next(); 
				if (cle.equals(symbol))
					return map.get(cle).iterator().next();
			}
		}
		return null;
	}

	public String toString() {
		return name;
	}

	@Override
	public String showTransitions() {
		return this + " a comme transitions " + map.toString();
	}

}
